import axios from 'axios';
import qs from 'query-string';

export interface BizId {
    id: number;
}

export interface BizRecord {
    id: number;
    name: string;
    image: string;
    avatar?: string;
    sort_by: number;
    created_at: number;
    updated_at: number;
    special_note?: string;
    biz_phone?: string;
    shop_address?: string;
    uscc?: string;
    service?: string;
    opentime?: number;
    close_time?: number;
    business_hours?: string;
    pos?: string;
    normal_discount?: number;
    goods_price_discount?: number;
    vip_discount?: number;
    desc?: string;
}

export interface BizListRes {
    list: BizRecord[];
    total: number;
}

export interface BizModel {
    id: number;
    name: string;
    image: string;
    sort_by: number;
}

export interface BizParams extends Partial<BizRecord> {
    current: number;
    pageSize: number;
    search?: string; // 搜索关键词
    tag_ids?: string; // 标签ID数组，逗号分隔
}

/**
 * 获取业务列表
 * @param params
 */
export function queryBizList(params: BizParams) {
    return axios.get<BizListRes>('/admin/biz/index', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 获取业务详情
 * @param params
 */
export function queryBizOne(params: BizId) {
    return axios.get<BizRecord>('/admin/biz/detail', {
        params,
        paramsSerializer: (obj) => {
            return qs.stringify(obj);
        },
    });
}

/**
 * 提交业务表单（创建/更新）
 * @param data
 */
export function submitBizForm(data: any) {
    return axios.post('/admin/biz/submit', data);
}

/**
 * 删除业务
 * @param id
 */
export function delBiz(id: number) {
    return axios.post('/admin/biz/delete', { id });
}

/**
 * 获取所有商家数据
 */
export function queryAllBiz() {
    return axios.get<BizRecord[]>('/admin/biz/all');
}

/**
 * 更新特别说明
 * @param data
 */
export function updateSpecialNote(data: { id: number; special_note: string }) {
    return axios.post('/admin/biz/updateSpecialNote', data);
}

/**
 * 获取商家已关联的标签
 * @param bizId
 */
export function getBizTags(bizId: number) {
    return axios.get('/admin/biz/getBizTags', { params: { biz_id: bizId } });
}

/**
 * 为商家添加标签
 * @param data
 */
export function addBizTag(data: { biz_id: number; tag_id: number }) {
    return axios.post('/admin/biz/addBizTag', data);
}

/**
 * 删除商家标签关联
 * @param data
 */
export function removeBizTag(data: { biz_id: number; tag_id: number }) {
    return axios.post('/admin/biz/removeBizTag', data);
}
